package general;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import utils.Parser;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.semanticweb.owlapi.model.OWLOntologyCreationException;

import queryManager.QrelManager;
import queryManager.Query;
import queryManager.QueryParser;


public class IHM extends JFrame {
	public static ArrayList<String> stopList;
	public static ArrayList<Query> queries; 
	//Qrels
	public static HashMap<Integer,HashMap<String ,Integer>> queryResults;
	
	private JButton run;
	private JButton run2;
	private JTextArea text;
	private HashMap<Integer,ArrayList<Double>> result;
	
	
	public IHM() throws IOException{
		stopList = Parser.parseStopListFile();
		queryResults = new HashMap<Integer, HashMap<String,Integer>>();
		queries = new ArrayList<Query>();
		initRequetes();
		initQrels();
		initComponent();
	}
	
	public void initComponent(){
		this.setTitle("Request Evaluator");
        this.setPreferredSize(new Dimension(900,700));
        this.setMinimumSize(new Dimension(800,700));
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLayout(new GridBagLayout());
        GridBagConstraints cons = new GridBagConstraints();
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                IHM.this.dispose();
            }
        });
        run = new JButton("Launch");
        run2 = new JButton("LaunchSemantique");
        run.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				Evaluator m;
				try {
					m = new Evaluator();
					result = m.getDatas();
					showDatas();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
        run2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				EvaluatorSemantic m;
				try {
					m = new EvaluatorSemantic();
					result = m.getDatas();
					showDatas();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (OWLOntologyCreationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
        text = new JTextArea("Requête\t\t précision 5\t\tprécision 10\t\tprécision 25\n\n");
        text.setFont(new Font("Arial", Font.BOLD, 17));
        JScrollPane sc = new JScrollPane(text);
        
        cons.insets = new Insets(0,0,40,0);
        cons.anchor = GridBagConstraints.CENTER;
        
//        cons.fill = GridBagConstraints.HORIZONTAL;
        cons.weightx = 1;
        cons.weighty = 0;
        cons.gridx = 0;
        cons.gridy = 0;
        this.add(run,cons);
        
        cons.gridx = 1;
        this.add(run2,cons);
        
        cons.weighty = 1;
        cons.weightx = 1;
        cons.fill = GridBagConstraints.BOTH;
        cons.anchor = GridBagConstraints.PAGE_START;
        cons.gridy = 1;
        cons.gridx = 0;
        cons.gridwidth = GridBagConstraints.REMAINDER;
        cons.gridheight = GridBagConstraints.REMAINDER;
        cons.fill = GridBagConstraints.BOTH;
        this.add(sc,cons);
        
        this.setVisible(true);
	}
	
	public void showDatas(){
		text.setText("");
		text.setText("Requête\t\t précision 5\t\tprécision 10\t\tprécision 25\n\n");
		Double moy1=0.0,moy2=0.0,moy3=0.0;
		for(Integer s: result.keySet()){
        	text.append("Requête "+s+":"+"\t\t  ");
        	for(Double d : result.get(s)){
        		text.append(d+"\t\t");
        	}
        	moy1 += result.get(s).get(0);
			moy2 += result.get(s).get(1);
			moy3 += result.get(s).get(2);
        	text.append("\n\n");
        }
        moy1 = ((int)((moy1/11)*1000))/1000.0;
        moy2 = ((int)((moy2/11)*1000))/1000.0;
        moy3 = ((int)((moy3/11)*1000))/1000.0;
      
        text.append("Moyenne: "+"\t\t"+moy1+"\t\t"+moy2+"\t\t"+moy3+"\t\t");
	}
	
	public static void main(String[] args) throws IOException{
		new IHM();
	}
	
	public void initQrels() throws IOException{
		//on recuper les resultats des requetes : les qrels
		QrelManager qrelsManager = new QrelManager("qrels","^qrel[0-9]+\\.txt$");
		queryResults.putAll(qrelsManager.getQrels());
//		return queryResults;
	}
	
	public void initRequetes(){
		QueryParser quer = new QueryParser("Requetes/queries.xml");
		queries.addAll(quer.getQueries());
	}
	
	
	
	
	
	
	
	

}
